package com.htj.admin.controller;

import cn.hutool.core.util.IdUtil;
import com.htj.admin.domain.Food;
import com.htj.admin.service.FoodServiceInf;
import com.htj.common.Result;
import com.htj.vo.PageListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "food")
@CrossOrigin
public class FoodController {
    @Autowired
    FoodServiceInf orderServiceInf;
    /**
     * 新增菜品
     * @param order
     * @return
     */
    @PostMapping("/save")
    public Result<String> add(@RequestBody Food order){
        boolean bool = orderServiceInf.add(order);
        return bool?Result.succ():Result.fail();
    }

    /**
     * 修改菜品
     * @param  order
     * @return
     */
    @PutMapping("/save")
    public Result<String> update(@RequestBody Food order){
        boolean bool = orderServiceInf.update(order);
        return bool?Result.succ():Result.fail();
    }
    /**
     * 分页数据
     * @param currentPage
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/pageList")
    public Result<PageListVo> pageList(@RequestParam String currentPage,
                                       @RequestParam String pageSize,
                                       @RequestParam String name){
        HashMap<String, String> map = new HashMap<>();
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        map.put("name",name);
        PageListVo<Food> pageList = orderServiceInf.pageList(map);
        return Result.succ(pageList);
    }

    /**
     * 删除菜品
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Result<String> delete(@PathVariable Integer id){
        boolean bool = orderServiceInf.delete(id);
        return bool?Result.succ():Result.fail();
    }

    /**
     * 批量删除
     * @param list
     * @return
     */
    @PostMapping("/deleteByIds")
    public Result<String> deleteByIds(@RequestBody List<Integer> list){
        boolean bool = true;
        for(Integer id : list){
            bool = orderServiceInf.delete(id);
        }
        return bool?Result.succ():Result.fail();
    }


    /**
     * 改变菜品状态
     * @param map
     * @return
     */
    @PostMapping("/changeStatus")
    public Result<String> changeStatus(@RequestBody Map<String,Integer> map){
        boolean bool = orderServiceInf.changeStatus(map);
        return bool?Result.succ():Result.fail();
    }

    @GetMapping("/getAll")
    public Result<List<Food>> getAll(){
        List<Food> list = orderServiceInf.getAll();
        return Result.succ(list);
    }
}
